package com.me.modules.sys.controller;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.page.Page;
import com.me.modules.common.utils.*;
import com.me.modules.sys.entity.TimetblWorkdateEntity;
import com.me.modules.sys.entity.TimetblWorktimeEntity;
import com.me.modules.sys.service.TimetblWorkdateService;
import com.me.modules.sys.service.TimetblWorktimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.me.modules.sys.entity.TimetblInfoEntity;
import com.me.modules.sys.service.TimetblInfoService;

import javax.servlet.http.HttpServletRequest;


/**
 * 工作时间模板
 *
 * @author lill
 * @email lill5270@163.com
 * @date 2019-01-08 16:56:49
 */
@Controller
@RequestMapping("/sys/timetblinfo")
public class TimetblInfoController extends BaseController{
	@Autowired
	private TimetblInfoService timetblInfoService;
	@Autowired
	private TimetblWorkdateService timetblWorkdateService;
	@Autowired
	private TimetblWorktimeService timetblWorktimeService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("timetblinfo:list")
	@SysLog("查询工作时间模板")
	public String  list(TimetblInfoEntity timetblInfoEntity, Model model , HttpServletRequest request){
		//查询列表数据
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<TimetblInfoEntity> page = timetblInfoService.findPage(timetblInfoEntity,pageNum);

		//PageUtils pageUtil = new PageUtils(timetblInfoList, total, query.getLimit(), query.getPage());
		model.addAttribute("timetblInfo",timetblInfoEntity);
		model.addAttribute("page",page);
		return "sys/timetblinfo/list";
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info")
	@RequiresPermissions("timetblinfo:info")
	@SysLog("查询工作时间列表")
	public String info( Model model,Long id, HttpServletRequest request){
		TimetblInfoEntity timetblInfo = timetblInfoService.queryObject(id);
		model.addAttribute("timetblInfo",timetblInfo);
		return "sys/timetblinfo/edit";
	}



	@RequestMapping(value = "save",method = RequestMethod.POST)
	@RequiresPermissions("timetblinfo:save")
	@ResponseBody
	public Result save(TimetblInfoEntity timetblInfo,Long [] timetblWorkdateIds,Long [] timetblWorktimeIds){
		if(timetblInfo.getId()==null) {
			timetblInfoService.save(timetblInfo);
		}else {
			timetblInfoService.update(timetblInfo);
		}
		if (timetblWorkdateIds!=null){
			for (Long id:timetblWorkdateIds) {
				TimetblWorkdateEntity timetblWorkdate = timetblWorkdateService.queryObject(id);
				timetblWorkdate.setTblId(timetblInfo.getId());
				timetblWorkdateService.update(timetblWorkdate);
			}
		}
		if (timetblWorkdateIds!=null) {
			for (Long id : timetblWorktimeIds) {
				TimetblWorktimeEntity timetblWorktime = timetblWorktimeService.queryObject(id);
				timetblWorktime.setTmpId(timetblInfo.getId());
				timetblWorktimeService.update(timetblWorktime);
			}
		}
		return Result.ok();
	}


	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("timetblinfo:update")
	public Result update(@RequestBody TimetblInfoEntity timetblInfo){
		timetblInfoService.update(timetblInfo);

		return Result.ok();
	}


	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Result edit(Long id){
		if(timetblInfoService.delete(id)<1){
			return Result.error("删除工作时间模板失败");
		}
		return Result.ok("删除工作时间模板成功");
	}
}
